package com.starlink.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.starlink.client.ras.RasClient;
import com.starlink.client.ras.request.ActivationSingleCodeActivationRequest;
import com.starlink.common.utils.AssertUtil;
import com.starlink.mapper.MallBooksMapper;
import com.starlink.mapper.SchoolbagBooksMapper;
import com.starlink.mapper.SchoolbagBooksStorageMapper;
import com.starlink.model.domain.*;
import com.starlink.model.dtomapper.SchoolbagBooksStorageDtoMapper;
import com.starlink.model.request.MallBooksRasRequest;
import com.starlink.model.request.SchoolbagBooksStorageRequest;
import com.starlink.model.util.SchoolbagBooksStorageConvert;
import com.starlink.model.vo.SchoolbagBooksStorageVO;
import com.starlink.service.MallBooksRasService;
import com.starlink.service.SchoolbagBooksStorageService;
import com.starlink.service.SchoolbagGroupBagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.starlink.utils.PageUtils.getOrderBy;

/**
 * @author zhanghong
 */
@Service
public class SchoolbagBooksStorageServiceImpl extends ServiceImpl<SchoolbagBooksStorageMapper, SchoolbagBooksStorageDO> implements SchoolbagBooksStorageService {
    @Autowired
    private SchoolbagBooksStorageMapper schoolbagBooksStorageMapper;
    @Autowired
    private SchoolbagBooksMapper schoolbagBooksMapper;
    @Autowired
    private MallBooksMapper mallBooksMapper;
    @Autowired
    private RasClient rasClient;
    @Autowired
    private SchoolbagGroupBagService schoolbagGroupBagService;
    @Autowired
    private MallBooksRasService mallBooksRasService;

    @Override
    public SchoolbagBooksStorageVO getByKey(long id) {
        SchoolbagBooksStorageVO schoolbagBooksStorageVO = SchoolbagBooksStorageDtoMapper.INSTANCE.do2VO(getById(id));
        return schoolbagBooksStorageVO;
    }

    @Override
    public PageInfo<SchoolbagBooksStorageVO> pageQuery(Pageable pageable,SchoolbagBooksStorageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<SchoolbagBooksStorageDO> list = schoolbagBooksStorageMapper.listQuery(request);
        List<SchoolbagBooksStorageVO> schoolbagBooksStorageVOList = SchoolbagBooksStorageConvert.buildSchoolbagBooksStorageVoList(list);
        return new PageInfo(schoolbagBooksStorageVOList);
    }

    @Override
    public List<SchoolbagBooksStorageVO> listQuery(SchoolbagBooksStorageRequest request) {
        List<SchoolbagBooksStorageDO> list = schoolbagBooksStorageMapper.selectDOList(SchoolbagBooksStorageDtoMapper.INSTANCE.request2DO(request));
        List<SchoolbagBooksStorageVO> schoolbagBooksStorageVOS = SchoolbagBooksStorageConvert.buildSchoolbagBooksStorageVoList(list);
        return schoolbagBooksStorageVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SchoolbagBooksStorageRequest request) {
        AssertUtil.assertNotNull(request.getRasCode(), "芯片码不能为空");
        AssertUtil.assertNotNull(request.getBooksId(), "书籍id不能为空");
        SchoolbagBooksDO schoolbagBooksDO = schoolbagBooksMapper.selectDOById(request.getBooksId());
        AssertUtil.assertNotNull(schoolbagBooksDO, "书籍不存在");

        // 校验RAS编码是否重复（书籍）
        boolean mallBookRasExists = mallBooksRasService.lambdaQuery().eq(MallBooksRasDO::getRasCode, request.getRasCode()).exists();
        AssertUtil.assertFalse(mallBookRasExists, "该RAS编码已存在");

        // 校验RAS编码是否重复（书包书籍）
        boolean storageExists = lambdaQuery().eq(SchoolbagBooksStorageDO::getRasCode, request.getRasCode()).exists();
        AssertUtil.assertFalse(storageExists, "该RAS编码已存在");

        // 校验RAS编码是否重复（书包）
        Boolean bagListExists = schoolbagGroupBagService.lambdaQuery().eq(SchoolbagGroupBagDO::getBagCode, request.getRasCode()).exists();
        AssertUtil.assertFalse(bagListExists, "该RAS编码已存在");

        // 存在性校验
        MallBooksDO mallBooksDO = mallBooksMapper.selectDOById(schoolbagBooksDO.getMallBooksId());
        AssertUtil.assertNotNull(mallBooksDO, "书籍不存在");
        AssertUtil.assertNotNull(mallBooksDO.getRasGoodsId(), "RAS审核还未通过!");

        // 是否审核通过校验
        MallBooksRasRequest auditRequest = new MallBooksRasRequest();
        auditRequest.setRasGoodsId(mallBooksDO.getRasGoodsId());
        AssertUtil.assertTrue(mallBooksRasService.check(auditRequest), "RAS审核还未通过!");

        // RAS激活
        ActivationSingleCodeActivationRequest activationRequest = new ActivationSingleCodeActivationRequest();
        activationRequest.setAbLabel(Lists.newArrayList(request.getRasUrl()));
        activationRequest.setGoodsId(mallBooksDO.getRasGoodsId().intValue());
        activationRequest.setWrapSpecs("1份/本");
        activationRequest.setActivationType("单码激活");
        rasClient.activationSingleCodeActivation(activationRequest);

        // 数据存储
        save(SchoolbagBooksStorageDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(SchoolbagBooksStorageRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(SchoolbagBooksStorageDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        schoolbagBooksStorageMapper.deleteDOByIds(Convert.toStrArray(ids));
        // TODO RAS芯片接触绑定
        return true;
    }
}
